//
//  TextViewLogger.m
//  LogManagerDemo
//
//  Created by US bike on 2017/7/25.
//  Copyright © 2017年 US bike. All rights reserved.
//

#import "TextViewLogger.h"
#import "TextViewLogFormatter.h"

static TextViewLogger *sharedInstance;


@implementation TextViewLogger

+ (instancetype) sharedInstance {
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[[self class] alloc] init];
    });
    
    return sharedInstance;
}

- (instancetype) init {
    
    if (!self) {
        return nil;
    }
    
    if (self = [super init]) {
        _logFormatter = [[TextViewLogFormatter alloc] init];
        [[LogManager shareInstance] logManagerEnable:YES];
        return self;
    }
    
    return nil;
}

- (void)logMessage:(DDLogMessage *)logMessage {
    
    NSString * message = _logFormatter ? [_logFormatter formatLogMessage:logMessage] : logMessage->_message;
    
    if (message) {
        //const char *msg = [message UTF8String];
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        switch (logMessage->_flag) {
            case DDLogFlagError     ://红
                [dic setValue:[UIColor redColor] forKey:@"logColor"];
                break;
            case DDLogFlagWarning   :
                [dic setValue:[UIColor orangeColor] forKey:@"logColor"];

                break;
            case DDLogFlagInfo      :
                [dic setValue:[UIColor blackColor] forKey:@"logColor"];

                break;
            case DDLogFlagDebug     :
                [dic setValue:[UIColor blueColor] forKey:@"logColor"];

                break;
            case DDLogFlagVerbose   :
            default                 :
                [dic setValue:[UIColor grayColor] forKey:@"logColor"];
                break;
        }
        
        [dic setValue:message forKey:@"logStr"];

        [[NSNotificationCenter defaultCenter] postNotificationName:TextViewLoggerNotificationName object:nil userInfo:dic];
    }
}


- (NSString *) loggerName {
    return @"textViewLogger";
}
@end
